package com.tarena.lbs.attach.dao.mapper;

import com.tarena.lbs.attach.pojo.po.AttachPO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AttachMapper {

    /**
     * 插入附件记录
     */
    @Insert("INSERT INTO attach (file_uuid, client_file_name, content_length, business_type, business_id, status, create_user_id, gmt_create) " +
            "VALUES (#{fileUuid}, #{clientFileName}, #{contentLength}, #{businessType}, #{businessId}, #{status}, #{createUserId}, #{gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(AttachPO attachPO);

    /**
     * 根据ID查询附件
     */
    @Select("SELECT * FROM attach WHERE id = #{id}")
    AttachPO selectById(Integer id);

    /**
     * 根据业务类型和业务ID查询附件
     */
    @Select("SELECT * FROM attach WHERE business_type = #{businessType} AND business_id = #{businessId}")
    List<AttachPO> selectByBusiness(@Param("businessType") Integer businessType, @Param("businessId") Integer businessId);

    /**
     * 更新附件状态
     */
    @Update("UPDATE attach SET status = #{status}, gmt_modified = #{gmtModified} WHERE id = #{id}")
    int updateStatus(AttachPO attachPO);

    /**
     * 批量删除附件
     */
    @Delete("<script>" +
            "DELETE FROM attach WHERE id IN " +
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int deleteByIds(@Param("ids") List<Integer> ids);

    /**
     * 绑定业务关系
     */
    @Update("UPDATE attach SET business_type = #{businessType}, business_id = #{businessId} WHERE id = #{id}")
    int bindBusiness(AttachPO attachPO);

    /**
     * 根据业务ID和业务类型删除附件
     */
    @Delete("DELETE FROM attach WHERE business_id = #{businessId} AND business_type = #{businessType}")
    int deleteByBusiness(@Param("businessId") String businessId, @Param("businessType") String businessType);

}
